﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TEAA;


public partial class FormatTable : System.Web.UI.Page
{
    SystemManager sm = SystemManager.GetSystemManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        //ViewState["activeDB"] = sm.GetActiveDatabase();
        //String activeDB = Context.Items["activeDB"].ToString();
        //sm.ActivateDatabase(activeDB);        
        Session["page"] = "FormatTable";
        if (Session["start"] == null)
        {
            ((SiteMaster)Master).setInitalDB();
        }
        else
        {
            if (IsPostBack)
            {                
                if ((bool)Session["SelectedChanged"])
                {
                    Session["SelectedChanged"] = false;
                }
                else
                {
                    ((SiteMaster)Master).fillddlDatabase();
                    ((SiteMaster)Master).setddlDatabaseSelectedItem();
                }                
            }
            else
            {                
                ((SiteMaster)Master).fillddlDatabase();
                ((SiteMaster)Master).setddlDatabaseSelectedItem();
                //String db = ((SiteMaster)Master).getViewState();                
                setInitialTable();
                setControlsVisibility(false);
                settbHosszEnabel();
                setFkDropdownsEnable(false);
                fillDropDownList(DropDownListTables);
                fillddlTipusok();
                cbUjTabla.Checked = false;

            }
        }
    }

    private void setInitialTable()
    {
        if (DropDownListTables.Items.Count > 0)
        {
            Session["table"] = DropDownListTables.Items[0].ToString();
        }
        else
        {
            Session["table"] = null;
        }
    }

    #region fill dropdowns
    public void fillddlTipusok()
    {
        ddlTipus1.Items.Clear();
        ddlTipus2.Items.Clear();
        ddlTipus3.Items.Clear();
        ddlTipus4.Items.Clear();
        ddlTipus5.Items.Clear();
        string[] enumNames = Enum.GetNames(typeof(DataType));
        foreach(string item in enumNames){
            ddlTipus1.Items.Add(item);
            ddlTipus2.Items.Add(item);
            ddlTipus3.Items.Add(item);
            ddlTipus4.Items.Add(item);
            ddlTipus5.Items.Add(item);
        }        
    }

    private void fillDropDownList(DropDownList ddl)
    {        
        try
        {
            List<string> tables = sm.GetTables();
            ddl.Items.Clear();
            foreach (string item in tables)
            {
                ddl.Items.Add(new ListItem(item.ToString()));
            }
        }
        catch(Exception ex){
            alertBox(ex);
        }
    }

    public void fillDropDownListTables()
    {
        clearTableData();
        try
        {
            List<string> tables = sm.GetTables();
            DropDownListTables.Items.Clear();
            foreach (string item in tables)
            {
                DropDownListTables.Items.Add(new ListItem(item.ToString()));
            }
            fillTablaAdatai(sm.GetActiveDatabase(), DropDownListTables.Items[0].ToString());
            //string db = ViewState["activeDB"].ToString();
            //fillTablaAdatai( db, DropDownListTables.Items[0].ToString());
        }
        catch (Exception ex)
        {
            alertBox(ex);
        }
    }

    private void fillDrowDownListAttribute(DropDownList ddlFill, DropDownList ddl)
    {
        //sm = SystemManager.GetSystemManager();
        string table = "";
        table = ddl.SelectedValue.ToString();     
        if (table == "")        
        {
            table = ddl.Items[0].ToString();
        }
            List<string> attributes = sm.GetAttributes(table);
            ddlFill.Items.Clear();
            foreach (string item in attributes)
            {
                ddlFill.Items.Add(new ListItem(item.ToString()));
            }        
    }
#endregion

    #region set Enable and Visibility
    public void setFkDropdownsEnable(bool state)
    {
        ddlFkTable1.Enabled = state;
        ddlFkTable2.Enabled = state;
        ddlFkTable3.Enabled = state;
        ddlFkTable4.Enabled = state;
        ddlFkTable5.Enabled = state;
        ddlFKAttr1.Enabled = state;
        ddlFKAttr2.Enabled = state;
        ddlFKAttr3.Enabled = state;
        ddlFKAttr4.Enabled = state;
        ddlFKAttr5.Enabled = state;
    }

    public void setControlsVisibility(bool state)
    {
        Label2.Visible = state;
        TextBoxTableName.Visible = state;
        Label1.Visible = !state;
        DropDownListTables.Visible = !state;
    }

    private void settbHosszEnabel()
    {
        tbHossz1.Enabled = false;
        tbHossz2.Enabled = false;
        tbHossz3.Enabled = false;
        tbHossz4.Enabled = false;
        tbHossz5.Enabled = false;
    }

    public void cleatTextBoxAttributes()
    {
        tbAttr1.Text = "";
        tbAttr2.Text = "";
        tbAttr3.Text = "";
        tbAttr4.Text = "";
        tbAttr5.Text = "";
    }

    public void cleartbHossz()
    {
        tbHossz1.Text = "";
        tbHossz2.Text = "";
        tbHossz3.Text = "";
        tbHossz4.Text = "";
        tbHossz5.Text = "";
    }

    public void clearcbPrimaryKey()
    {
        cbPrimKey1.Checked = false;
        cbPrimKey2.Checked = false;
        cbPrimKey3.Checked = false;
        cbPrimKey4.Checked = false;
        cbPrimKey5.Checked = false;
    }

    public void clearcbNull()
    {
        cbIsnull1.Checked = false;
        cbIsnull2.Checked = false;
        cbIsnull3.Checked = false;
        cbIsnull4.Checked = false;
        cbIsnull5.Checked = false;
    }

    public void clearcbForeignKey()
    {
        cbForeignKey1.Checked = false;
        cbForeignKey2.Checked = false;
        cbForeignKey3.Checked = false;
        cbForeignKey4.Checked = false;
        cbForeignKey5.Checked = false;
    }

    public void clearddlForeignKey()
    {
        ddlFkTable1.Items.Clear();
        ddlFkTable2.Items.Clear();
        ddlFkTable3.Items.Clear();
        ddlFkTable4.Items.Clear();
        ddlFkTable5.Items.Clear();
        ddlFKAttr1.Items.Clear();
        ddlFKAttr2.Items.Clear();
        ddlFKAttr3.Items.Clear();
        ddlFKAttr4.Items.Clear();
        ddlFKAttr5.Items.Clear();
    }

    public void clearcbUniqKey()
    {
        cbUniqKey1.Checked = false;
        cbUniqKey2.Checked = false;
        cbUniqKey3.Checked = false;
        cbUniqKey4.Checked = false;
        cbUniqKey5.Checked = false;
    }

    public void clearCbTorol()
    {
        cbDelete1.Checked = false;
        cbDelete2.Checked = false;
        cbDelete3.Checked = false;
        cbDelete4.Checked = false;
        cbDelete5.Checked = false;
    }

    public void clearTableData()
    {
        cleatTextBoxAttributes();
        fillddlTipusok();
        cleartbHossz();
        settbHosszEnabel();
        clearcbPrimaryKey();
        clearcbNull();
        clearcbForeignKey();
        clearddlForeignKey();
        clearCbTorol();
        clearcbUniqKey();
    }

    #endregion

    protected void cbUjTabla_CheckedChanged(object sender, EventArgs e)
    {
        if (cbUjTabla.Checked == true) // create new database
        {
            setControlsVisibility(true);
            btnDeleteTable.Visible = false;
            clearTableData();
        }
        else
        {
            setControlsVisibility(false);
            btnDeleteTable.Visible = true;
            fillDropDownList(DropDownListTables);
            clearTableData();
        }
    }

    public Boolean validTableName(string table)
    {
        if (table.Trim().Length != 0)
            return true;
        else return false;
    }

    public Boolean validAttributeName(string attribute)
    {        
        if ((attribute.Trim().Length != 0) && (Regex.IsMatch(attribute, "[^0-9]")))
            return true;
        else return false;
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        ((SiteMaster)Master).setddlDatabaseSelectedItem();
        if (cbUjTabla.Checked)
        {
            //string activDB = Session["activDB"].ToString();
            //sm = SystemManager.GetSystemManager();
            bool sikeresLetrehozas = false;
            try
            {
                //sm.ActivateDatabase(activDB);
                try
                {
                    if (validTableName(TextBoxTableName.Text))
                    {
                        sm.CreateTable(TextBoxTableName.Text);
                        try
                        {
                            string tableName = TextBoxTableName.Text.ToString();
                            createAttributes(tableName);
                            sikeresLetrehozas = true;
                            //cbUjTabla.Checked = false;
                            //cbUjTabla_CheckedChanged(sender, e);
                        }
                        catch (Exception ex)
                        {
                            sm.DropTable(TextBoxTableName.Text.ToString());
                            alertBox(ex);                                                        
                        }
                    }
                    else
                    {
                        labelHelytelenTablanev.Visible = true;
                    }
                }
                catch (Exception ex)
                {
                    alertBox(ex);
                    //sm.DropTable(TextBoxTableName.Text.ToString());
                    // reszlegesen beszurt tabla torlese
                }
            }
            catch (Exception ex)
            {
                alertBox(ex);
            }
            //((SiteMaster)Master).fillddlDatabase();
            //((SiteMaster)Master).setddlDatabaseSelectedItem();
            if (sikeresLetrehozas)
            {
                alertSikeres();
                cbUjTabla.Checked = false;
                setControlsVisibility(false);
                fillDropDownList(DropDownListTables); 
                // talan elotte jo lenne torolni mindent
                clearTableData();
                fillTablaAdatai(sm.GetActiveDatabase(), DropDownListTables.SelectedValue.ToString());
            }
        }
        else // letezo tabla updateje
        {

        }
    }   

    public void fillTablaAdatai(string dbName, string tableName)
    {
        List<string> attributes = sm.GetAttributes(tableName);
        int attrCount = attributes.Count;
        switch (attrCount)
        {
            case 1:
                fillAttr1(tableName, attributes[0], tbAttr1, ddlTipus1, tbHossz1, cbPrimKey1, cbIsnull1, cbForeignKey1, ddlFkTable1, ddlFKAttr1, cbUniqKey1);
                break;
            case 2:
                fillAttr1(tableName, attributes[0], tbAttr1, ddlTipus1, tbHossz1, cbPrimKey1, cbIsnull1, cbForeignKey1, ddlFkTable1, ddlFKAttr1, cbUniqKey1);
                fillAttr1(tableName, attributes[1], tbAttr2, ddlTipus2, tbHossz2, cbPrimKey2, cbIsnull2, cbForeignKey2, ddlFkTable2, ddlFKAttr2, cbUniqKey2);
                break;
            case 3:
                fillAttr1(tableName, attributes[0], tbAttr1, ddlTipus1, tbHossz1, cbPrimKey1, cbIsnull1, cbForeignKey1, ddlFkTable1, ddlFKAttr1, cbUniqKey1);
                fillAttr1(tableName, attributes[1], tbAttr2, ddlTipus2, tbHossz2, cbPrimKey2, cbIsnull2, cbForeignKey2, ddlFkTable2, ddlFKAttr2, cbUniqKey2);                
                fillAttr1(tableName, attributes[2], tbAttr3, ddlTipus3, tbHossz3, cbPrimKey3, cbIsnull3, cbForeignKey3, ddlFkTable3, ddlFKAttr3, cbUniqKey3);
                break;
            case 4:
                fillAttr1(tableName, attributes[0], tbAttr1, ddlTipus1, tbHossz1, cbPrimKey1, cbIsnull1, cbForeignKey1, ddlFkTable1, ddlFKAttr1, cbUniqKey1);
                fillAttr1(tableName, attributes[1], tbAttr2, ddlTipus2, tbHossz2, cbPrimKey2, cbIsnull2, cbForeignKey2, ddlFkTable2, ddlFKAttr2, cbUniqKey2);                
                fillAttr1(tableName, attributes[2], tbAttr3, ddlTipus3, tbHossz3, cbPrimKey3, cbIsnull3, cbForeignKey3, ddlFkTable3, ddlFKAttr3, cbUniqKey3);
                fillAttr1(tableName, attributes[3], tbAttr4, ddlTipus4, tbHossz4, cbPrimKey4, cbIsnull4, cbForeignKey4, ddlFkTable4, ddlFKAttr4, cbUniqKey4);
                break;
            case 5:
                fillAttr1(tableName, attributes[0], tbAttr1, ddlTipus1, tbHossz1, cbPrimKey1, cbIsnull1, cbForeignKey1, ddlFkTable1, ddlFKAttr1, cbUniqKey1);
                fillAttr1(tableName, attributes[1], tbAttr2, ddlTipus2, tbHossz2, cbPrimKey2, cbIsnull2, cbForeignKey2, ddlFkTable2, ddlFKAttr2, cbUniqKey2);                
                fillAttr1(tableName, attributes[2], tbAttr3, ddlTipus3, tbHossz3, cbPrimKey3, cbIsnull3, cbForeignKey3, ddlFkTable3, ddlFKAttr3, cbUniqKey3);
                fillAttr1(tableName, attributes[3], tbAttr4, ddlTipus4, tbHossz4, cbPrimKey4, cbIsnull4, cbForeignKey4, ddlFkTable4, ddlFKAttr4, cbUniqKey4);
                fillAttr1(tableName, attributes[4], tbAttr5, ddlTipus5, tbHossz5, cbPrimKey5, cbIsnull5, cbForeignKey5, ddlFkTable5, ddlFKAttr5, cbUniqKey5);
                
                break;
        }
        
    }

    private void fillAttr1(string table, string attr, TextBox tbAttr, DropDownList ddlTipus, TextBox tbHossz, CheckBox cbPrimKey,
        CheckBox cbIsnull, CheckBox cbForeignKey, DropDownList ddlFkTable, DropDownList ddlFKAttr, CheckBox cbUniqeKey)
    {
        DataTable dt = getAttributeProperties(table, attr);
        tbAttr.Text = attr;

        ddlTipus.ClearSelection();        
        ListItem li = ddlTipus.Items.FindByValue(dt.Rows[0]["type"].ToString());
        if (li != null) li.Selected = true;

        tbHossz.Text = dt.Rows[0]["length"].ToString();
        tbHossz.Enabled = true;
        cbPrimKey.Checked = (bool)dt.Rows[0]["primaryKey"];
        cbIsnull.Checked = (bool)dt.Rows[0]["isNull"];
        cbForeignKey.Checked = (bool)dt.Rows[0]["foreignKey"];
        if (cbForeignKey.Checked)
        {
            fillDropDownList(ddlFkTable);
            ddlFkTable.ClearSelection();
            li = ddlFkTable.Items.FindByValue(dt.Rows[0]["fkTable"].ToString());
            if (li != null) li.Selected = true;
            fillDrowDownListAttribute(ddlFKAttr, ddlFkTable);
            ddlFKAttr.ClearSelection();
            li = ddlFKAttr.Items.FindByValue(dt.Rows[0]["fkAttr"].ToString());
            if (li != null) li.Selected = true;
            ddlFkTable.Enabled = true;
            ddlFKAttr.Enabled = true;
        }
        cbUniqeKey.Checked = (bool)dt.Rows[0]["uniqKey"];
    }

    public DataTable getAttributeProperties(string table, string attr)
    {
        DataType type = sm.GetAttributeDataType(table, attr);
        int length = sm.GetAttributeLength(table, attr);
        bool isPrimaryKey = sm.IsPrimaryKey(table, attr);
        bool isNull = sm.IsAttributeIsNull(table, attr);
        bool isForeignKey = sm.IsForeignKey(table, attr);
        string fkTable = "";
        string fkAttr = "";
        if (isForeignKey)
        {
            fkTable = sm.GetForeignKeyReferenceTable(table, attr);
            fkAttr = sm.GetForeignKeyReferenceAttribute(table, attr);
        }
        bool isUniqKey = sm.IsUniqueKey(table, attr);

        DataTable dt = new DataTable();
        DataColumn dataColumn = new DataColumn("type", System.Type.GetType("System.String")); 
        dt.Columns.Add(dataColumn);
        dataColumn = new DataColumn("length", System.Type.GetType("System.Int32"));
        dt.Columns.Add(dataColumn);
        dataColumn = new DataColumn("primaryKey", System.Type.GetType("System.Boolean"));
        dt.Columns.Add(dataColumn);
        dataColumn = new DataColumn("isNull", System.Type.GetType("System.Boolean"));
        dt.Columns.Add(dataColumn);
        dataColumn = new DataColumn("foreignKey", System.Type.GetType("System.Boolean"));
        dt.Columns.Add(dataColumn);
        dataColumn = new DataColumn("fkTable", System.Type.GetType("System.String"));
        dt.Columns.Add(dataColumn);
        dataColumn = new DataColumn("fkAttr", System.Type.GetType("System.String"));
        dt.Columns.Add(dataColumn);
        dataColumn = new DataColumn("uniqKey", System.Type.GetType("System.Boolean"));
        dt.Columns.Add(dataColumn);

        DataRow dr = dt.NewRow();
        dr["type"] = type;
        dr["length"] = length;
        dr["primaryKey"] = isPrimaryKey;
        dr["isNull"] = isNull;
        dr["foreignKey"] = isForeignKey;
        dr["fkTable"] = fkTable;
        dr["fkAttr"] = fkAttr;
        dr["uniqKey"] = isUniqKey;
        dt.Rows.Add(dr);

        return dt;
    }

    private void createAttributes(String tableName)
    {
        string attrName = "";

        //1. attribute
        int length = 0;
        if (tbHossz1.Enabled == true) length = Convert.ToInt32(tbHossz1.Text);
        else length = DataTypeUtils.GetDataTypeLength((DataType)Enum.Parse(typeof(DataType), ddlTipus1.SelectedValue));
        if (validAttributeName(tbAttr1.Text))
        {
            attrName = tbAttr1.Text.ToString();            
            sm.CreateAttribute(tableName, attrName, (DataType)Enum.Parse(typeof(DataType), ddlTipus1.SelectedValue), cbIsnull1.Checked, length);
            bool uniq = false;
            if (cbUniqKey1.Checked) {sm.SetUniqueKey(tableName, attrName); uniq = true;}
            if (cbPrimKey1.Checked)
            {
                sm.SetPrimaryKey(tableName, attrName);
                sm.CreateIndex(tableName, attrName, IndexType.BPlusTree, uniq);
            }
            if (cbForeignKey1.Checked){
                string fktable = ddlFkTable1.SelectedValue.ToString();
                if (fktable == "") fktable = ddlFkTable1.Items[0].ToString();
                string fkattr = ddlFKAttr1.SelectedValue.ToString();
                if (fkattr == "") fkattr = ddlFKAttr1.Items[0].ToString();
                sm.SetForeignKey(tableName, attrName, fktable, fkattr);
            }                                  
        }

        //2. attribute
        //length = 0;
        if (tbHossz2.Enabled == true) length = Convert.ToInt32(tbHossz2.Text);
        else length = DataTypeUtils.GetDataTypeLength((DataType)Enum.Parse(typeof(DataType), ddlTipus2.SelectedValue));
        if (validAttributeName(tbAttr2.Text))
        {
            attrName = tbAttr2.Text.ToString();
            bool uniq = false;
            if (cbUniqKey2.Checked) { sm.SetUniqueKey(tableName, attrName); uniq = true; } 
            sm.CreateAttribute(tableName, attrName, (DataType)Enum.Parse(typeof(DataType), ddlTipus2.SelectedValue), cbIsnull2.Checked, length);
            if (cbPrimKey2.Checked)
            {
                sm.SetPrimaryKey(tableName, attrName);
                sm.CreateIndex(tableName, attrName, IndexType.BPlusTree, uniq);
            }
            if (cbForeignKey2.Checked)
            {
                string fktable = ddlFkTable2.SelectedValue.ToString();
                if (fktable == "") fktable = ddlFkTable2.Items[0].ToString();
                string fkattr = ddlFKAttr2.SelectedValue.ToString();
                if (fkattr == "") fkattr = ddlFKAttr2.Items[0].ToString();
                sm.SetForeignKey(tableName, attrName, fktable, fkattr);
            }                    
        }

        //3. attribute
        //length = 0;
        if (tbHossz3.Enabled == true) length = Convert.ToInt32(tbHossz3.Text);
        else length = DataTypeUtils.GetDataTypeLength((DataType)Enum.Parse(typeof(DataType), ddlTipus3.SelectedValue));
        if (validAttributeName(tbAttr3.Text))
        {
            attrName = tbAttr3.Text.ToString();
            sm.CreateAttribute(tableName, attrName, (DataType)Enum.Parse(typeof(DataType), ddlTipus3.SelectedValue), cbIsnull3.Checked, length);
            bool uniq = false;
            if (cbUniqKey3.Checked) { sm.SetUniqueKey(tableName, attrName); uniq = true; }
            if (cbPrimKey3.Checked)
            {
                sm.SetPrimaryKey(tableName, attrName);
                sm.CreateIndex(tableName, attrName, IndexType.BPlusTree, uniq);
            }
            if (cbForeignKey3.Checked)
            {
                string fktable = ddlFkTable3.SelectedValue.ToString();
                if (fktable == "") fktable = ddlFkTable3.Items[0].ToString();
                string fkattr = ddlFKAttr3.SelectedValue.ToString();
                if (fkattr == "") fkattr = ddlFKAttr3.Items[0].ToString();
                sm.SetForeignKey(tableName, attrName, fktable, fkattr);
            }            
        }

        //4. attribute
        //length = 0;
        if (tbHossz4.Enabled == true) length = Convert.ToInt32(tbHossz4.Text);
        else length = DataTypeUtils.GetDataTypeLength((DataType)Enum.Parse(typeof(DataType), ddlTipus4.SelectedValue));
        if (validAttributeName(tbAttr4.Text))
        {
            attrName = tbAttr4.Text.ToString();
            sm.CreateAttribute(tableName, attrName, (DataType)Enum.Parse(typeof(DataType), ddlTipus4.SelectedValue), cbIsnull4.Checked, length);
            bool uniq = false;
            if (cbUniqKey4.Checked) {sm.SetUniqueKey(tableName, attrName); uniq = true;}
            if (cbPrimKey4.Checked) 
            { 
                   sm.SetPrimaryKey(tableName, attrName); 
                   sm.CreateIndex(tableName, attrName, IndexType.BPlusTree, uniq); 
            }
            if (cbForeignKey4.Checked)
            {
                string fktable = ddlFkTable4.SelectedValue.ToString();
                if (fktable == "") fktable = ddlFkTable4.Items[0].ToString();
                string fkattr = ddlFKAttr4.SelectedValue.ToString();
                if (fkattr == "") fkattr = ddlFKAttr4.Items[0].ToString();
                sm.SetForeignKey(tableName, attrName, fktable, fkattr);
            }            
        }

        //5. attribute
        //length = 1;
        if (tbHossz5.Enabled == true) length = Convert.ToInt32(tbHossz5.Text);
        else length = DataTypeUtils.GetDataTypeLength((DataType)Enum.Parse(typeof(DataType), ddlTipus5.SelectedValue));
        if (validAttributeName(tbAttr5.Text))
        {
            attrName = tbAttr5.Text.ToString();
            sm.CreateAttribute(tableName, attrName, (DataType)Enum.Parse(typeof(DataType), ddlTipus5.SelectedValue), cbIsnull5.Checked, length);
            bool uniq = false;
            if (cbUniqKey5.Checked) { sm.SetUniqueKey(tableName, attrName); uniq = true; }
            if (cbPrimKey5.Checked) {
                sm.SetPrimaryKey(tableName, attrName);
                sm.CreateIndex(tableName, attrName, IndexType.BPlusTree, uniq);
            }
            if (cbForeignKey5.Checked)
            {
                string fktable = ddlFkTable5.SelectedValue.ToString();
                if (fktable == "") fktable = ddlFkTable5.Items[0].ToString();
                string fkattr = ddlFKAttr5.SelectedValue.ToString();
                if (fkattr == "") fkattr = ddlFKAttr5.Items[0].ToString();
                sm.SetForeignKey(tableName, attrName, fktable, fkattr);
            }            
        }
    }    

    private void alertBox(Exception ex)
    {
        Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
        string errorMessage = "";
        Exception exception = ex;
        while (exception != null)
        {
            errorMessage += exception.Message + "\\n";
            exception = exception.InnerException;
        }
        Response.Write("alert('" + errorMessage + "');");
        Response.Write("</script>");
    }

    private void alertSikeres()
    {
        Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
        Response.Write("alert('A muvelet sikeresen vegre lett hajtva! ');");
        Response.Write("</script>");
    }

    #region ddl Tipus SelectedIndexChanged
    protected void ddlTipus1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTipus1.SelectedValue.ToString() == DataType.Text.ToString()) tbHossz1.Enabled = true;
        else { tbHossz1.Text = ""; tbHossz1.Enabled = false; }
    }

    protected void ddlTipus2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTipus2.SelectedValue.ToString() ==DataType.Text.ToString()) tbHossz2.Enabled = true;
        else { tbHossz2.Text = ""; tbHossz2.Enabled = false; }
    }

    protected void ddlTipus3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTipus3.SelectedValue.ToString() ==DataType.Text.ToString()) tbHossz3.Enabled = true;
        else { tbHossz3.Text = ""; tbHossz3.Enabled = false; }
    }

    protected void ddlTipus4_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTipus4.SelectedValue.ToString() ==DataType.Text.ToString()) tbHossz4.Enabled = true;
        else { tbHossz4.Text = ""; tbHossz4.Enabled = false; }
    }

    protected void ddlTipus5_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTipus5.SelectedValue.ToString() ==DataType.Text.ToString()) tbHossz5.Enabled = true;
        else { tbHossz5.Text = ""; tbHossz5.Enabled = false; }
    }
    #endregion

    protected void DropDownListTables_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["table"] = DropDownListTables.SelectedValue.ToString();
        clearTableData();
        fillTablaAdatai(sm.GetActiveDatabase(), DropDownListTables.SelectedValue.ToString());
        //fillTablaAdatai(ViewState["activeDB"].ToString(), DropDownListTables.SelectedValue.ToString());
    }

    #region CheckBox Foreign Key
    protected void cbForeignKey1_CheckedChanged(object sender, EventArgs e)
    {
        if (cbForeignKey1.Checked)
        {
            ddlFkTable1.Enabled = true;
            ddlFKAttr1.Enabled = true;
            fillDropDownList(ddlFkTable1);
            fillDrowDownListAttribute(ddlFKAttr1, ddlFkTable1);
        }
        else
        {
            ddlFkTable1.Items.Clear();
            ddlFKAttr1.Items.Clear();
            ddlFkTable1.Enabled = false;
            ddlFKAttr1.Enabled = false;
        }
    }
    
    protected void cbForeignKey2_CheckedChanged(object sender, EventArgs e)
    {
        if (cbForeignKey2.Checked)
        {
            ddlFkTable2.Enabled = true;
            ddlFKAttr2.Enabled = true;
            fillDropDownList(ddlFkTable2);
            fillDrowDownListAttribute(ddlFKAttr2, ddlFkTable2);
        }
        else
        {
            ddlFkTable2.Items.Clear();
            ddlFKAttr2.Items.Clear();
            ddlFkTable2.Enabled = false;
            ddlFKAttr2.Enabled = false;
        }
    }

    protected void cbForeignKey3_CheckedChanged(object sender, EventArgs e)
    {
        if (cbForeignKey3.Checked)
        {
            ddlFkTable3.Enabled = true;
            ddlFKAttr3.Enabled = true;
            fillDropDownList(ddlFkTable3);
            fillDrowDownListAttribute(ddlFKAttr3, ddlFkTable3);
        }
        else
        {
            ddlFkTable3.Items.Clear();
            ddlFKAttr3.Items.Clear();
            ddlFkTable3.Enabled = false;
            ddlFKAttr3.Enabled = false;
        }
    }

    protected void cbForeignKey4_CheckedChanged(object sender, EventArgs e)
    {
        if (cbForeignKey4.Checked)
        {
            ddlFkTable4.Enabled = true;
            ddlFKAttr4.Enabled = true;
            fillDropDownList(ddlFkTable4);
            fillDrowDownListAttribute(ddlFKAttr4, ddlFkTable4);
        }
        else
        {
            ddlFkTable4.Items.Clear();
            ddlFKAttr4.Items.Clear();
            ddlFkTable4.Enabled = false;
            ddlFKAttr4.Enabled = false;
        }
    }

    protected void cbForeignKey5_CheckedChanged(object sender, EventArgs e)
    {
        if (cbForeignKey5.Checked)
        {
            ddlFkTable5.Enabled = true;
            ddlFKAttr5.Enabled = true;
            fillDropDownList(ddlFkTable5);
            fillDrowDownListAttribute(ddlFKAttr5, ddlFkTable5);
        }
        else
        {
            ddlFkTable5.Items.Clear();
            ddlFKAttr5.Items.Clear();
            ddlFkTable5.Enabled = false;
            ddlFKAttr5.Enabled = false;
        }
    }
    #endregion

    #region ddl Foreign Key Table SelectedIndexChanged
    protected void ddlFkTable1_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillDrowDownListAttribute(ddlFKAttr1, ddlFkTable1);
    }

    protected void ddlFkTable2_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillDrowDownListAttribute(ddlFKAttr2, ddlFkTable2);
    }

    protected void ddlFkTable3_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillDrowDownListAttribute(ddlFKAttr3, ddlFkTable3);
    }

    protected void ddlFkTable4_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillDrowDownListAttribute(ddlFKAttr4, ddlFkTable4);
    }

    protected void ddlFkTable5_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillDrowDownListAttribute(ddlFkTable5, ddlFkTable5);
    }

#endregion

    protected void btnDeleteTable_Click(object sender, EventArgs e)
    {
        try
        {
            string activeDB = sm.GetActiveDatabase();
            //if (activeDB==null) activeDB = ViewState["activeDB"].ToString();
            sm.ActivateDatabase(activeDB);
            string tableName = DropDownListTables.SelectedValue.ToString();
            if (tableName == null) tableName = DropDownListTables.Items[0].ToString();
            if (tableName != null) sm.DropTable(tableName);
            fillDropDownListTables();
        }
        catch (Exception ex)
        {
            alertBox(ex);
        }
    }

    protected void btnDeleteAttr_Click(object sender, EventArgs e)
    {
        try
        {
            string databaseName = sm.GetActiveDatabase();
            string tableName = DropDownListTables.SelectedValue.ToString();
            if (tableName == null) tableName = DropDownListTables.Items[0].ToString();
            if (cbDelete1.Checked) sm.DropAttribute(tableName, tbAttr1.Text);
            if (cbDelete2.Checked) sm.DropAttribute(tableName, tbAttr2.Text);
            if (cbDelete3.Checked) sm.DropAttribute(tableName, tbAttr3.Text);
            if (cbDelete4.Checked) sm.DropAttribute(tableName, tbAttr4.Text);
            if (cbDelete5.Checked) sm.DropAttribute(tableName, tbAttr5.Text);
            clearTableData();
            fillTablaAdatai(databaseName, tableName);
        }
        catch (Exception ex)
        {
            alertBox(ex);
        }
    }    

}
